package com.qingxin.sys.service;

import java.util.Date;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.SysPermission;
import com.qingxin.sys.entity.SysUser;
import com.qingxin.sys.entity.SysUserRights;
import com.qingxin.sys.mvc.SysError;

/**
 * 用户权限
 * @author Administrator
 *
 */
@Service
public class SysUserRightsService extends BaseService<SysUserRights, String> {

	/**
	 * 保存指定用户的权限
	 * @param userid
	 * @param perids
	 * @param loginUser
	 * @return
	 */
	@CacheEvict(value="userRightsCache", allEntries = true)
	public SysError saveUserRights(String userid, String[] perids, LoginUser loginUser){
		evictCache("userRightsCache", null);
		//删除现有权限
		deleteUserRights(userid);
		
		//保存新设置的权限
		if(perids==null || perids.length==0){
			return null;
		}
		
		Date now = new Date();
		String create_by = loginUser.getLogin_name();
		for(String perid : perids){
			SysUserRights rights = new SysUserRights(new SysUser(userid), new SysPermission(perid), create_by, now);
			this.save(rights);
		}
		return null;
	}
	
	
	/**
	 * 删除指定用户的所有权限
	 * @param userid
	 * @return
	 */
	@CacheEvict(value="userRightsCache", allEntries = true)
	public SysError deleteUserRights(String userid){
		evictCache("userRightsCache", null);
		deleteByHql("delete from SysUserRights r where r.sysUser.userid=?", userid);
		return null;
	}
	
	
	
}
